﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using ExtAspNet;
using Ylh.BLL;
using System.Data;

public partial class Module_RoleManger_RoleTree2 : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnCancel.OnClientClick = ActiveWindow.GetHideReference();

        if (!IsPostBack)
        {
            LoadTv();
        }

    }

    private void LoadTv()
    {

        StaffBLL blls = new StaffBLL(DbConnectionStr);
        DataTable dtStaff = blls.GetStaff(Convert.ToInt32(Request.QueryString["StaffID"].ToString()));


        RoleBLL bll = new RoleBLL(DbConnectionStr);
        DataTable dt = bll.GetRoleInFo(-1);

        ExtAspNet.TreeNode Tn = new ExtAspNet.TreeNode();
        Tn.Text = "角色列表";
        Tn.Expanded = true;

        Tree1.Nodes.Add(Tn);

        ExtAspNet.TreeNode tn;
        foreach (DataRow dr in dt.Rows)
        {
            tn = new ExtAspNet.TreeNode();
            tn.Text = dr["RoleName"].ToString();
            tn.NodeID = dr["RoleID"].ToString();
            tn.EnableCheckBox = true;

            if (dtStaff.Rows[0]["RoleID"] != null)
            {
                if (dtStaff.Rows[0]["RoleID"].ToString() == dr["RoleID"].ToString())
                {
                    tn.Checked = true;
                }

            }
            Tn.Nodes.Add(tn);
        }


    }


    protected void btncomfirm_Click(object sender, EventArgs e)
    {

        ExtAspNet.TreeNode[] nodes = Tree1.GetCheckedNodes();

        if (nodes.Length == 0)
        {
            Alert.ShowInParent("请先选择角色");
            return;
        }

        if (nodes.Length > 1)
        {
            Alert.ShowInParent("只能选择一个角色");
            return;
        }

        StaffBLL bll = new StaffBLL(DbConnectionStr);
        if (!bll.UpdateStaffRole(Convert.ToInt32(Request.QueryString["StaffID"].ToString()), nodes[0].NodeID))
        {
            Alert.ShowInParent("角色更新错误,请联系管理员");
            return;

        }
        if (Request.QueryString["GetCode"] != null)
        {
            PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(nodes[0].Text, nodes[0].NodeID) + ActiveWindow.GetHideReference());
        }
        else
        {
            PageContext.RegisterStartupScript(ActiveWindow.GetHideReference());
        }



    }
}